//
//  SDEntryViewController.swift
//  Seed
//
//  Created by Samuel on 2017/6/5.
//  Copyright © 2017年 RedSoft. All rights reserved.
//

import UIKit
import SVProgressHUD

class SDEntryViewController: YMBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: false)
    }

    //随便逛逛：很多功能无法使用，仅供浏览
    @IBAction func hangout(_ sender: UIButton) {
        appDelegate.setLoginStatus(true)
       
    }
    //微信登录：如果已经绑定手机号，则直接进系统，否则绑定手机号，并完成注册
    @IBAction func wexinLogin(_ sender: UIButton) {
        UMSocialManager.default().getUserInfo(with: .wechatTimeLine, currentViewController: self) { (result:Any?, error:Error?) in
            if let resp = result as? UMSocialUserInfoResponse{
                printLog(resp.uid)
                printLog(resp.iconurl)
                //如果已经绑定手机号，则直接进系统，否则绑定手机号，并完成注册
                SDNetworkTool.shared.weixinLogin(withUnionId: resp.uid, success: { (account:SDAccount) in
                    //注册成功，直接登录
                    SVProgressHUD.showSuccess(withStatus: "登陆成功")
                    //保存当前账号信息
                    SDAccountTool.shared.saveAccount(account)
                    //保存登陆状态，跳转登陆
                    appDelegate.setLoginStatus(true)
                }, failure: {
                    let registerVc = UIStoryboard.instantiateViewController("Login", withIdentifier: "SDRegisterViewController") as! SDRegisterViewController
                    registerVc.type = .BindWechat
                    registerVc.nickName = resp.name
                    registerVc.imgUrl = resp.iconurl
                    registerVc.uid = resp.uid
                    self.navigationController?.show(registerVc, sender: nil)
                })
            }
        }
    }
    
}
